package br.com.obardavez.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import br.com.obardavez.util.impl.FileUtil;


public class FileUtilImpl implements FileUtil {

	
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see com.digicade.android.commons.util.impl.FileUtil#getCurrentPath()
	 */
	@Override
	public String getCurrentPath() {
		final String SUFFIX_TO_REMOVE = "com";
		String fullPath = FileUtilImpl.class.getClassLoader().getResource("com")
				.getFile();
		String currentPath = fullPath.replace(SUFFIX_TO_REMOVE, "");
		currentPath = currentPath.replace("/", "\\");
		String finalCurrentPath = currentPath.substring(1);
		return finalCurrentPath;
	}

	/* (non-Javadoc)
	 * @see com.digicade.android.commons.util.impl.FileUtil#parseInputStreamToString(java.io.InputStream)
	 */
	@Override
	public String parseInputStreamToString(InputStream inputStream) throws IOException {

		java.io.Writer writer = new java.io.StringWriter();
		char[] buffer = new char[1024];
		java.io.Reader reader = new java.io.BufferedReader(
				new java.io.InputStreamReader(inputStream, "UTF-8"));
		int n;
		while ((n = reader.read(buffer)) != -1) {
			writer.write(buffer, 0, n);
		}
		inputStream.close();
		return writer.toString();
	}
	
	/* (non-Javadoc)
	 * @see com.digicade.android.commons.util.impl.FileUtil#includeEndSlash(java.lang.String)
	 */
	@Override
	public String includeEndSlash(String path) {
		if (path != null && !path.endsWith("\\") && !path.endsWith("/")
				&& !isFile(path)) {
			path += File.separator;
		}
		return path;
	}
	
	/* (non-Javadoc)
	 * @see com.digicade.android.commons.util.impl.FileUtil#isFile(java.lang.String)
	 */
	@Override
	public Boolean isFile(String path) {
		Boolean isFile = Boolean.FALSE;
		if (path != null && !path.equals("")) {
			String[] tokens = path.split("\\\\|/");
			isFile = tokens[tokens.length - 1].contains(".");
		}
		return isFile;
	}
	
}